//
//  ViewController.m
//  AFNetworking
//
//  Created by Justsoso on 16/4/11.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "ViewController.h"

#import <AFNetworking.h>

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *usernameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@property (weak, nonatomic) IBOutlet UIProgressView *progressBar;
@property (weak, nonatomic) IBOutlet UILabel *progressLable;
@property (weak, nonatomic) IBOutlet UIImageView *selImageView;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - GET 方式
- (IBAction)getBtnPressed:(id)sender
{
    NSString *urlStr = @"http://120.25.226.186:32812/login";
    
    // 参数放到一个字典里
    NSDictionary *dict = @{@"username":self.usernameTextField.text,@"pwd":self.passwordTextField.text,@"type":@"JSON"};
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    // 1. 请求时提交的数据格式
    mgr.requestSerializer = [AFJSONRequestSerializer serializer];// JSON
    
    // 2. 返回的数据格式
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];// 二进制数据
    
    [mgr GET:urlStr parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功");
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",dict);
        
        if (dict[@"error"])
        {
            NSLog(@"登录失败:%@",dict[@"error"]);
        }
        else
        {
            NSLog(@"登录成功:%@",dict[@"error"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];
}

#pragma mark - POST 方式
- (IBAction)postBtnPressed:(id)sender
{
    NSString *urlStr = @"http://120.25.226.186:32812/login";
    
    // 参数放到一个字典里
    NSDictionary *dict = @{@"username":self.usernameTextField.text,@"pwd":self.passwordTextField.text,@"type":@"JSON"};
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    // 1. 请求时提交的数据格式
    mgr.requestSerializer = [AFJSONRequestSerializer serializer];// JSON
    
    // 2. 返回的数据格式
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];// 二进制数据
    
    [mgr POST:urlStr parameters:dict success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功");
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",dict);
        
        if (dict[@"error"])
        {
            NSLog(@"登录失败:%@",dict[@"error"]);
        }
        else
        {
            NSLog(@"登录成功:%@",dict[@"error"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];
}

#pragma mark - 上传文件
- (IBAction)uploadBtnPressed:(id)sender
{
    NSString *urlStr = @"http://120.25.226.186:32812/upload";
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    // 1. 请求时提交的数据格式
//    mgr.requestSerializer = [AFJSONRequestSerializer serializer];// JSON
    
    // 2. 返回的数据格式
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];// 二进制数据
    
    [mgr POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"艾斯" withExtension:@"jpg"];
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        [formData appendPartWithFileData:data name:@"file" fileName:@"艾斯.jpg" mimeType:@"image/png"];
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"请求成功");
        
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",dict);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求失败");
    }];
    
}
- (IBAction)downBtnPressed:(id)sender {
}
- (IBAction)uploadImageBtnPressed:(id)sender {
}

@end
